class Artist < ActiveRecord::Base
  #Relations
  has_and_belongs_to_many :releases
  has_and_belongs_to_many :songs
  has_many	:artists_genres, :dependent => :destroy
  has_many	:genres, :through => :artists_genres, :source => :genre
  has_many	:name_aliases, :dependent => :destroy
  has_many	:edition_credits, :dependent => :destroy
  has_many	:song_credits, :dependent => :destroy
  has_many 	:edits, :as => :editable, :order => "created_at DESC"
  has_many	:reviews, :as => :reviewable
  has_many	:images, :as => :photographable, :dependent => :destroy
  has_many	:favorites, :as => :favoritable
  has_many	:artist_dates, :dependent => :destroy

  belongs_to	:location
  belongs_to	:forum, :dependent => :destroy

  #Bands and Members
  has_many :bands,   :foreign_key => 'member_id', 
                     :class_name => 'Membership',
                     :dependent => :destroy
  has_many :members, :foreign_key => 'band_id',
                     :class_name => 'Membership', 
                     :dependent => :destroy

  #Virtual Attributes
  attr_accessor :city_name
  attr_accessor :state_name
  attr_accessor :country_name
  
  
  #Callbacks
  after_update :create_edit
  after_create :new_entry_edit
  before_save :add_forum
  before_save :update_location

  #Validations
  validates_presence_of :name, :forum_id
  validates_inclusion_of :gender,
                         :in => %w{ Male Female },
                         :message => "should be male or female",
                         :allow_blank => true,
                         :allow_nil => true

  #Overload the to_string operator to return the
  # artists name
  def to_s
    return name
  end

  #Creates pretty URLs
  def to_param
    "#{id}-#{name.parameterize}"
  end
  
  def memberships
    if self.is_group == true
	  return self.members
	else
	  return self.bands
	end
  end
  
  #
  def primary_image
    if images.empty?
      return nil 
    else
      images.first.image
    end
  end
  
  #
  def city
    #if location
      return location.city if location
	#end
  end
  
  #
  def state
    #if location
      return location.state if location
	#end
  end
  
  #
  def country
    #if location
      return location.country if location
	#end
  end
  
  def ordered_dates
	artist_dates.sort_by{|x| [x.year, x.month, x.day]}
  end
  
  #Retrieves an array of releases of a particular type
  def get_releases(type)
    releaseList = Array.new

    if type == 'All'
      releaseList = releases
    else
      releases.each do |release|
        if release.release_type == type
          releaseList << release
        end
      end
    end
    
    return releaseList
  end

  #Returns an array of all release types associated
  # with this artist
  def release_types
    types = ['All']

    #loops threw each release and adds its type to the array
    releases.each do |release|
      types << release.release_type
    end

    #returns only the uniques
    return types.uniq
  end

  private

  #
  #
  def add_forum
    if self.new_record?
      title = self.name + "'s Forum"
      forum = Forum.new(:title => title)
      forum.save!
      self.forum_id = forum.id
    end
  end

  #
  def update_location
    #If new location data is present, link the new location to the artist
	
    if !city_name.nil? || !state_name.nil? || !country_name.nil?
	  new_location = Location.city_equals(city_name).state_equals(state_name).country_equals(country_name).first
	  
	  if new_location.nil?
	    #didn't return anything, make one
		create_location(:city => city_name,
						:state => state_name,
						:country => country_name)
	  elsif new_location != self.location
	    #link it up
		self.location_id = new_location.id
	  end
	end
  end
  
  #
  #
  def new_entry_edit
      entry = Hash.new
      entry.store("created", [self.name])
      Edit.create_edit(entry, self)
  end

  #
  #
  def create_edit
    if self.changed?
      #puts changes
      Edit.create_edit(self.changes, self)
    end
  end
end
